# 1. 依赖安装阶段
FROM node:18-alpine AS deps
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci

# 2. 构建阶段
FROM node:18-alpine AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
# 生成构建产物
RUN npm run build

# 3. 生产运行阶段 (Runner)
FROM node:18-alpine AS runner
WORKDIR /app

ENV NODE_ENV production

# 创建非 root 用户提高安全性
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs

# ✨ 关键：只复制 standalone 文件夹
# copy public 文件夹 (静态资源)
COPY --from=builder /app/public ./public
# copy .next/static (JS/CSS chunks) 到正确的路径
COPY --from=builder /app/.next/static ./.next/static
# copy 独立打包出来的 server 和依赖
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./

USER nextjs

EXPOSE 3000
ENV PORT 3000

# 注意：这里直接运行 node server.js，而不是 npm start
CMD ["node", "server.js"]